<?php
session_start();

if(empty($_SESSION["log"])){
	header('Location: login.php');
}

include_once("connexion.php");


if (!empty($_POST["mail"]))
{
	$mail = $_POST["mail"];


	if (!preg_match("#^[a-z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$#", $mail)) 
	{
		echo "L'email n'a pas le bon format.";
		header  ("Refresh: 5");
	}
	else
	{
		$query = "UPDATE user SET mail='$mail' WHERE id='".$_GET['id']."';";
		mysqli_query($db,$query) or die('Erreur changement mail !'.$query.'<br/>'.mysqli_error($db));
	}
	
	
}
if (!empty($_POST['phone']))
{
	$phone = $_POST['phone'];
	if (!(strlen($phone)) == 10)
		echo "Numéro de téléphone invalide";
	$query = "UPDATE user SET phone='$phone' WHERE id='".$_GET['id']."';";
	mysqli_query($db, $query) or die('Erreur changement téléphone'.$query.'<br/>'.mysqli_error($db));

}

if (!empty($_POST['mdp']))
{
	$mdp = sha1('gz'.$_POST['mdp']);
	$query = "UPDATE user SET mdp='$mdp' WHERE id='".$_GET['id']."';";
	mysqli_query($db, $query) or die('Erreur changement mot de passe'.$query.'<br/>'.mysqli_error($db));
}

if (isset($_GET['id']))
{
	$query_mail = "SELECT mail FROM user WHERE id='".$_GET['id']."';";
	$query_mdp = "SELECT mdp FROM user WHERE id='".$_GET['id']."';";
	$query_phone = "SELECT phone FROM user WHERE id='".$_GET['id']."';";
	$res_mail = mysqli_query($db, $query_mail);
	$res_mdp = mysqli_query($db, $query_mdp);
	$res_phone = mysqli_query($db, $query_phone);
	if ($res_phone == false)
		echo "res_phone false";
	if ($res_mdp == false)
		echo "res_mdp false";
	if ($res_mail == false)
		echo "res_mail false";
	$tab_mail = mysqli_fetch_array($res_mail);
	$tab_phone = mysqli_fetch_array($res_phone);
	$tab_mdp = mysqli_fetch_array($res_mdp);

}



?>

<!DOCTYPE html>
<html>
    <head>
		  <link rel="stylesheet" type="text/css" href="style.css"/>
      <meta charset="utf-8" />
      <meta name="description" content="Free Bed ou comment trouver un endroit pour dormir en 2 cliques."/>
      <title>Free Bed</title>
    </head>
    <body>
      <div id="conteneur">
    		<div id="header">
    			<?php include_once("header.php"); ?>
        </div><br/>
    		<div id="contenu">
				<table>
					<form method="post">
						<tr>
							<td colspan="2"> <?php echo $tab_mail[0]; ?> </td>
							<td colspan="2" align="center">
								<input type="email" id="mail" name="mail" placeholder="Adresse mail"/>
							</td>
							<td></td>
						</tr>
						<tr>
							<td colspan="2"> <?php echo '0'.$tab_phone[0]; ?> </td>
							<td colspan="2" align="center">
								<input type="phone" placeholder="N° de Téléphone" name="phone"/>
							</td>
							<td></td>
						</tr>
						<tr>
							<td colspan="2"> Nouveaux mot de passe </td>
							<td colspan="2" align="center">
								<input type="text" placeholder="Mot de passe" name="mdp">

							</td>
						</tr>
						<tr>
							<td>Retaper votre mot de passe</td>
							<td colspan="2" align="center">
								<input type="text" placeholder="Confirmez votre mdp" name="mdp">

							</td>
						</tr>
						<td>
							<td colspan="2" align="center">
								<input type="submit" value="Valider" name="">
							</td>
						</tr>

					</form>
				</table>
			</div>
    		<div id="footer">
    			<?php include_once("footer.html"); ?>
    		</div>
      </div>
    </body>
</html>